<?php

class RequiredLabelsFormatterTable extends sfWidgetFormSchemaFormatterTable
{
  protected
    $requiredLabelClass = 'required';

  public function generateLabel($name, $attributes = array())
  {
    // loop up to find the "required_fields" option
    $widget = $this->widgetSchema;
    $requiredFields = array();
    do {
      $fields = (array) $widget->getOption('required_fields');
      if(!empty($fields)){
          $requiredFields = $fields;
      }
    } while ($widget = $widget->getParent());

    // add a class (non-destructively) if the field is required
    if (in_array($this->widgetSchema->generateName($name), $requiredFields))
    {
      $attributes['class'] = isset($attributes['class']) ?
        $attributes['class'].' '.$this->requiredLabelClass :
        $this->requiredLabelClass;
    }

    return parent::generateLabel($name, $attributes);
  }
}
